package com.ibeeking.gmc.global.g.rest.controller.v1;


import com.ibeeking.gmc.global.g.rest.service.IDatasourceConfigService;
import com.ibeeking.gmc.global.service.common.param.DatasourceConfigParam;
import com.ibeeking.gmc.global.service.common.param.DatasourceConfigTestParam;
import com.ibeeking.gmc.global.service.common.query.DatasourceConfigPageQuery;
import com.ibeeking.gmc.global.service.common.query.DatasourceConfigQuery;
import com.ibeeking.gmc.global.service.common.vo.DatasourceConfigPageVO;
import com.ibeeking.gmc.global.service.common.vo.DatasourceConfigVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName TemplateDatabaseController控制器
 * @Description
 * @Author ibeeking
 * @Date 2021-03-02 09:07
 **/
@RestController
@RequestMapping("/g/datasourceConfig/v1")
public class DatasourceConfigController {

    @Resource
    private IDatasourceConfigService datasourceConfigServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "有效数据库连接查询")
    @PostMapping(value = "/list")
    public List<DatasourceConfigVO> list(@RequestBody DatasourceConfigQuery query) {
        return datasourceConfigServiceImpl.list(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "数据库连接分页查询")
    @PostMapping(value = "/pageList")
    public Page<DatasourceConfigPageVO> pageList(@RequestBody DatasourceConfigPageQuery query) {
        return datasourceConfigServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "数据库连接新增")
    @PostMapping(value = "/add")
    public Boolean add(@RequestBody @Validated(BaseValidationGroup.Insert.class) DatasourceConfigParam param) {
        return datasourceConfigServiceImpl.add(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "数据库连接修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) DatasourceConfigParam param) {
        return datasourceConfigServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "数据库连接查看")
    @GetMapping(value = "/view/{id}")
    public DatasourceConfigVO view(@PathVariable("id") Long id) {
        return datasourceConfigServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "数据库连接修改状态")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return datasourceConfigServiceImpl.changeStatus(id, status);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "数据库连接删除")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return datasourceConfigServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "数据库连接测试")
    @PostMapping(value = "/connectionTest")
    public Boolean connectionTest(@RequestBody @Validated DatasourceConfigTestParam param) {
        return datasourceConfigServiceImpl.connectionTest(null, param);
    }
}
